setup_environment() {
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0
    export PATH=$PATH:$JAVA_HOME/bin
    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    
    export MAVEN_HOME=/home/work/apache-maven-3.3.9
    export PATH=$PATH:$MAVEN_HOME/bin
    
    export NODE_HOME=/home/work/node-v16.5.0-linux-x64
    export PATH=$PATH:$NODE_HOME/bin
}


clone_repository() {
    local repo_dir="/home/work/git_workspace"
    cd $repo_dir || exit 1
    rm -rf $repo_dir/*
    git clone git@gitee.com:yanjiuzongyuan/zysdp-lightning.git
    cd zysdp-lightning || exit 1
    git checkout prod
    echo "-------------Git代码克隆完成-------------"
}

build_frontend() {
    echo "-------------开始编译打包前端-------------"
    cd /home/work/git_workspace/zysdp-lightning/ruoyi-ui || exit 1

    npm install --registry=https://registry.npmmirror.com --legacy-peer-deps
    npm run build:prod

    rm -rf /home/work/zysdp-lightning_app/dist
    cp -r dist/ /home/work/zysdp-lightning_app/
    echo "-------------前端编译并传输完成-------------"
    sleep 3
}

build_backend() {
    echo "-------------开始编译后端-------------"
    cd /home/work/git_workspace/zysdp-lightning || exit 1
    mvn clean install
    echo "-------------后端编译完成-------------"
    sleep 3
}

transfer_backend_services() {
    echo "-------------本地后端服务传输-------------"
    local app_dir="/home/work/zysdp-lightning_app"
    
    cp -r /home/work/git_workspace/zysdp-lightning/ruoyi-admin/target/ruoyi-admin.jar $app_dir/
    
    echo "-------------本地后端服务传输完成-------------"
}


start_backend_service() {
    /home/work/cicd/backend_start.sh
    echo "-------------本地后端服务启动完成-------------"
}


main() {
    setup_environment
    clone_repository
    build_frontend
    build_backend
    transfer_backend_services
    start_backend_service
}


main